<?php
namespace lib\base;
use \think\Controller;
use think\Db;
use think\paginator\driver\Bootstrap;
use think\Url;
use think\View;
/**
 * 控制器基类
 *
 * @author zhangqiquan
 */
class BaseController extends Controller{
    protected $site_wap='site';
    protected $parseTemplate='';
    protected $shop_id='';
    protected $member_id='';
    protected $member_name='';
    protected function getField($app_name,$db_name,$out_data=[]){
        $incluefile=EXTEND_PATH.'dbstruct/'.$app_name.'/'.$db_name.'.php';
        if(!file_exists($incluefile)) return \trigger_error($incluefile.'文件不存在',E_USER_WARNING);
        $dbstruct= require $incluefile;
        //字段过滤
        if($dbstruct['Stru']&&  is_array($dbstruct['Stru'])){
            foreach($dbstruct['Stru'] as $k=>$v){
                if(in_array($k, $out_data))unset ($dbstruct['Stru'][$k]);
            }
        }
        return $dbstruct['Stru'];
    }
    protected function searchGetField($app_name,$db_name,$out_data=[]){
        $incluefile=EXTEND_PATH.'dbstruct/'.$app_name.'/'.$db_name.'.php';
        if(!file_exists($incluefile)) return \trigger_error($incluefile.'文件不存在',E_USER_WARNING);
        $dbstruct= require $incluefile;
        //字段过滤
        if($dbstruct['Stru']&&  is_array($dbstruct['Stru'])){
            foreach($dbstruct['Stru'] as $k=>$v){
                if(in_array($k, $out_data))unset ($dbstruct['Stru'][$k]);
                if($v['search_row_allow']==false||$v['search_row_allow']=='false')unset ($dbstruct['Stru'][$k]);
            }
        }
        return $dbstruct['Stru'];
    }
    protected function getPostKey($app_name,$db_name,$out_data=[],$post){
        $arr=[];
        $getfield=$this->getfield($app_name, $db_name, $out_data);
        if($getfield&&  is_array($getfield)){
            foreach($getfield as $k=>$v){
                if(isset($post[$k])){
                    $arr[$k]=$post[$k];
                }
            }
        }
        return $arr;
    }

    /**
 * 获取字段 并且排除 字段上面的in_list false
 * @param type $app_name
 * @param type $db_name
 * @param type $out_data
 * @return type
 */
    protected function dbstructField($app_name,$db_name,$out_field=[]){
        $incluefile=EXTEND_PATH.'dbstruct/'.$app_name.'/'.$db_name.'.php';
        if(!file_exists($incluefile)) return \trigger_error($incluefile.'文件不存在',E_USER_WARNING);
        $dbstruct= require $incluefile;
        //字段过滤
        if($dbstruct['Stru']&&  is_array($dbstruct['Stru'])){
            foreach($dbstruct['Stru'] as $k=>$v){
                if((isset($v['auto_increment'])&&$v['auto_increment']=='true')||(isset($v['in_list'])&&$v['in_list']!='true')||  in_array($k, $out_field))unset ($dbstruct['Stru'][$k]);
            }
        }
        return $dbstruct['Stru'];
    }
    
    protected function finder($app_name,$table_name,$where=[],$arguments=[],$obj,$element_id,$out_field=[],$order_by='desc'){
        $get=  input();
        $limit=20;
        if(isset($get['limit']))$limit=$get['limit'];
        $getfield=$this->dbstructField($app_name, $table_name,$out_field);
        if(isset($get['field_key'])&&$get['field_key']&&isset($get['field_val'])){
            $where2[$get['field_key']]=['like','%'.$get['field_val'].'%'];
            $list_obj = $obj->where($where)->where($where2)->field($element_id.' as id,'.implode(',', array_keys($getfield)))->order($order_by)->paginate($limit,false,['query'=>$get]);
        }else if(isset ($get['advanced'])&&  count($get)>=2){
            unset($get['advanced']);
            $where2='';
            foreach($get as $k=>$v){
                if($v){
                    $where2.=' '.$k.'="'.$v.'" and';
                }
            }
            $where2=rtrim($where2, 'and');
            $list_obj = $obj->where($where)->where($where2)->field($element_id.' as id,'.implode(',', array_keys($getfield)))->order($order_by)->paginate($limit,false,['query'=>$get]);
        }else{
            $list_obj = $obj->where($where)->field($element_id.' as id,'.implode(',', array_keys($getfield)))->order($order_by)->paginate($limit,false,['query'=>$get]);
        }
        $list=$list_obj->toarray();
        if(file_exists(APP_PATH.$app_name.'/finder/'.$table_name.'.php')){
            $eval='$table_name_finder=new app\\'.$app_name.'\\finder\\'.$table_name.'();';
            eval($eval);
            $tags=$table_name_finder->tags;
            if($tags&&  is_array($tags)&&$list['data']){
                foreach($list['data'] as $k=>$v){
                    foreach ($tags as $k1 => $v1) {
                        $getfield[$k1]=['label'=>$v1];
                        $list['data'][$k][$k1]=$table_name_finder->$k1($v);
                    }
                }
            }
        }
        $this->assign('dbstructure', $getfield);
        $this->assign('getfield', $getfield);
        $this->assign('searchGetField', $this->searchGetField($app_name, $table_name, $out_field));
        $this->assign('list', $list);
        $this->assign('page', $list_obj->render());
        $this->assign('arguments', $arguments);
        return $this->fetch(EXTEND_PATH.'view/base/'.$this->site_wap.'/finder.html');
    }
    
   protected function editFieldData($app_name,$table_name,$out_data=[],$obj,$where=[]){
        $Brand=$this->getfield($app_name, $table_name,$out_data);
        $data=  $obj::where($where)->find();
        $data=$data->toarray();
        if($Brand&&  is_array($Brand)){
            foreach($Brand as $k=>$v){
                if(isset($v['auto_increment'])&&$v['auto_increment']=='true'){
                    $Brand[$k]['input_type']='hidden';
                }
                $Brand[$k]['default']=$data[$k];
            }
        }
        return $Brand;
   }
    public function finderDetail(){
        $get=  input('');
        $this->assign('get', http_build_query($get));
        $this->assign('isAjax', \think\Request::instance()->isAjax());
        return $this->fetch(EXTEND_PATH.'view/base/'.$this->site_wap.'/finder_detail_frame.html');
    }
    public function finderDetailHtml(){
        $get=  input('');
        $finder_path=  explode('/', $get['finder_path']);
        if(!file_exists(APP_PATH.$finder_path[0].'/finder/'.$finder_path[1].'.php'))exit("finder不存在");
        $eval='$finderobj=new \\app\\'.$finder_path[0].'\\finder\\'.$finder_path[1].'();';
        eval($eval);
        $detail=$finderobj->detail;
        $this->assign('get', $get);
        $this->assign('detail', $detail);
        $this->assign('isAjax', $get['isAjax']);
        if ($get['isAjax']){
            return $this->fetch(EXTEND_PATH.'view/base/'.$this->site_wap.'/finder_detail_ajax.html');
        }else{
            return $this->fetch(EXTEND_PATH.'view/base/'.$this->site_wap.'/finder_detail.html');
        }
    }
    public function detail(){
        $get=  input('');
        $finder_path=explode('/', $get['finder_path']);
        $eval='$obj='."new \\app\\".$finder_path[0].'\\finder\\'.$finder_path[1].'();';
        eval($eval);
        $eval1='$row=$obj->'.$finder_path[2].'("'.$get['id'].'");';
        eval($eval1);
        echo $row;die;
    }
}
